//
//  HomeBasseViewController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/13.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "HomeBasseViewController.h"
#import "HomeFocusController.h"
#import "HomeRecomedController.h"
#import "HomeSerachController.h"
#import "HomeQuestionAndAnswerController.h"
#import "HomeCityController.h"
#import "HomeVideoController.h"
#import "HomeSmallVideoController.h"
#import "HomeIndustryDetailsController.h"
#import "HomeDeliciousFoodController.h"
#import "NavigationBarTitleView.h"
#import "VideoCaptureViewController.h"


@interface HomeBasseViewController ()<SGPageTitleViewDelegate, SGPageContentCollectionViewDelegate>
/**顶部按钮视图*/
@property (nonatomic,strong)SGPageTitleView *pageTitleView;
/**顶部按钮视图文字配置文件*/
@property (nonatomic,strong)SGPageTitleViewConfigure *configure;
/**底部视图管理器*/
@property (nonatomic,strong)SGPageContentCollectionView *pageContentCollectionView;
/**对 navigationItem.titleView 的包装，为的是 让View 占据整个视图宽度*/
@property (nonatomic,strong)NavigationBarTitleView *navView;
/**搜索视图*/
@property (nonatomic,strong)UILabel*serachLabel;
/**右侧下拉框点击按钮*/
@property (nonatomic,strong)UIButton * rightBUtton;
/**下拉框按钮数组*/
@property (nonatomic,strong)NSMutableArray*items;
/**顶部按钮数组*/
@property (nonatomic,strong)NSMutableArray*titleArr;
/**底部子控制器数组*/
@property (nonatomic,strong)NSMutableArray*childArr;
@end

@implementation HomeBasseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setSerachView];
    [self setupNavigationBar];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setNavBarBackgroundAndNotClearColor];
    [self.navigationController.navigationBar setShadowImage:[UIImage createImageWithColor:KWhiteColor]];
    self.StatusBarStyle = UIStatusBarStyleDefault;
}
- (void)setupNavigationBar {
    self.navigationItem.title = @"";
    self.view.backgroundColor = UIColorFromRGB(0xF5F7F5);
    self.navigationItem.titleView = self.navView;
    [self.view addSubview:self.pageContentCollectionView];
}
/**
 设置搜索栏
 */
-(void)setSerachView{
    UIView * view = [self createSerbar];
    
    [self.view addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(44);
    }];
}
#pragma mark 点击搜索框跳转进入搜索页面
-(void)tapTextFileAction{
    BasseNavigationController * nav = [[BasseNavigationController alloc] initWithRootViewController:[HomeSerachController new]];
    [self.navigationController presentViewController:nav animated:YES completion:nil];
}
#pragma mark SGPageTitleViewDelegate, SGPageContentCollectionViewDelegate
- (void)pageTitleView:(SGPageTitleView *)pageTitleView selectedIndex:(NSInteger)selectedIndex {
    if (selectedIndex != 3) {
        HomeVideoController *all4 =self.childArr[3];
        [all4 pausePlay];
    }
    [self.pageContentCollectionView setPageContentCollectionViewCurrentIndex:selectedIndex];
}
- (void)pageContentCollectionView:(SGPageContentCollectionView *)pageContentCollectionView progress:(CGFloat)progress originalIndex:(NSInteger)originalIndex targetIndex:(NSInteger)targetIndex {
    if (targetIndex != 3) {
        HomeVideoController *all4 =self.childArr[3];
        [all4 pausePlay];
    }
    [self.pageTitleView setPageTitleViewWithProgress:progress originalIndex:originalIndex targetIndex:targetIndex];
}
#pragma mark ---懒加载
-(UIView * )createSerbar{
    UIView * view = UIView.new;
    view.backgroundColor = UIColor.whiteColor;
    
    UIView * view1 = UIView.new;
    view1.backgroundColor = UIColorFromRGB(0xF5F7F5);
    ViewRadius(view1, 4);
    view1.userInteractionEnabled = YES;
    
    [view1 addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapTextFileAction)]];

    
    [view addSubview:view1];
    self.rightBUtton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.rightBUtton setBackgroundImage:IMAGE_NAMED(@"home_search_right_btn") forState:UIControlStateNormal];
    [view addSubview:self.rightBUtton];

    kWeakSelf(weakSelf);
    [self.rightBUtton addActionHandler:^{
        MenuView *view = [MenuView menuWithActions:weakSelf.items width:150 relyonView:weakSelf.rightBUtton];
        view.backgroundColor = RGBA(39, 51, 35, 0.8);
        view.textColor = KWhiteColor;
        view.textFont = PingFangSC_Regular(16);
        view.maxDisplayCount = 6;
        view.separatorColor = KRedColor;
        [view show];
        
        
        view.handler = ^(NSIndexPath * _Nonnull indexPath) {
            HomeVideoController * vc = weakSelf.childArr[3];
            [vc pausePlay];
            if (indexPath.row == 2) {
                [weakSelf.navigationController presentViewController:[[BasseNavigationController alloc]initWithRootViewController:[VideoCaptureViewController new]] animated:YES completion:nil];
            }else if (indexPath.row == 3){
                [weakSelf.navigationController presentViewController:[[BasseNavigationController alloc]initWithRootViewController:[VideoCaptureViewController new]] animated:YES completion:nil];

            }
        };
    }];
    
    
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-53);
        make.height.mas_equalTo(30);
    }];
    
    UIImageView * leftImage = [[UIImageView alloc] initWithImage:IMAGE_NAMED(@"ic_nav_search")];
    [view1 addSubview:leftImage];
    [view1 addSubview:self.serachLabel];
    

    [leftImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(8);
    }];
    
    [self.serachLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(leftImage.mas_right).offset(4);
        make.top.bottom.mas_equalTo(0);
    }];
    [self.rightBUtton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-16);
//        make.width.height.mas_equalTo(35);
        make.centerY.mas_equalTo(0);
    }];
    
    
    return view;
}

-(UILabel*)serachLabel{
    if(!_serachLabel){
        _serachLabel=[UILabel new];
        _serachLabel.text = @"热门搜索、热门搜索";
        _serachLabel.textColor = UIColorFromRGB(0x959E97);
        _serachLabel.font = PingFangSC_Light(14);
        _serachLabel.userInteractionEnabled = YES;
        [_serachLabel addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapTextFileAction)]];
    }
    return _serachLabel;
}
-(NSMutableArray *)items{
    if (!_items) {
        _items =[NSMutableArray array];
        MenuAction *action = [MenuAction  actionWithTitle:@"图文" pushVC:@"ReleaseTextAndImageController"];
        MenuAction *action1 = [MenuAction actionWithTitle:@"文章"  pushVC:@"ReleaseArticleController"];
        MenuAction *action2 = [MenuAction actionWithTitle:@"发视频"  pushVC:@""];
        MenuAction *action3 = [MenuAction actionWithTitle:@"小视频"  pushVC:@""];
        MenuAction *action4 = [MenuAction actionWithTitle:@"提问"  pushVC:@"AskQuestionsController"];
        MenuAction *action5 = [MenuAction actionWithTitle:@"美食"  pushVC:@"ReleaseDeliciousFoodController"];

        NSArray * array  = @[action,action1,action2,action3,action4,action5];
        [_items addObjectsFromArray:array];
    }
    return _items;
}
-(SGPageTitleViewConfigure*)configure{
    if(!_configure){
        _configure = [SGPageTitleViewConfigure pageTitleViewConfigure];
        _configure.titleTextZoom = YES;
        _configure.showIndicator = NO;
        _configure.titleTextZoomRatio = 0.5;
        _configure.titleAdditionalWidth = 30;
        _configure.titleGradientEffect = YES;
        _configure.titleSelectedColor = UIColorFromRGB(0x009966);
//        _configure.titleFont = PingFangSC_Medium(16);
    //    configure.titleSelectedFont = PingFangSC_Medium(22);
        _configure.bottomSeparatorColor = UIColorFromRGB(0xE9EEE7);
//        _configure.showBottomSeparator = NO;
    }
    return _configure;
}
-(NavigationBarTitleView*)navView{
    if(!_navView){
        // 对 navigationItem.titleView 的包装，为的是 让View 占据整个视图宽度
        _navView=[[NavigationBarTitleView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 44)];
        [_navView addSubview:self.pageTitleView];
    }
    return _navView;
}
-(SGPageTitleView*)pageTitleView{
    if(!_pageTitleView){
        // 这里的 - 10 是为了让 SGPageTitleView 超出父视图，给用户一种效果体验
        _pageTitleView=[SGPageTitleView pageTitleViewWithFrame:CGRectMake(- 10, 0, KScreenWidth-10, 44) delegate:self titleNames:self.titleArr configure:self.configure];
        _pageTitleView.backgroundColor = [UIColor clearColor];
        
    }
    return _pageTitleView;
}
-(NSMutableArray *)childArr{
    if (!_childArr) {
        
        HomeFocusController *all1 =[[HomeFocusController alloc]init];
        all1.type=TopicTypeFocus;
        
        HomeRecomedController *all2 =[[HomeRecomedController alloc]init];
        all2.type=TopicTypeRecommended;
        
        HomeCityController *all3 =[[HomeCityController alloc]init];
        all3.type=TopicTypeCity;
        
        kWeakSelf(weakSelf);
        all3.selecteBlock = ^(NSString * _Nonnull cityName) {
            [weakSelf.pageTitleView resetTitle:cityName forIndex:2];
        };

        HomeVideoController *all4 =[[HomeVideoController alloc]init];
        all4.type=TopicTypeVideo;

        HomeSmallVideoController *all5 =[[HomeSmallVideoController alloc]init];
        all5.type=TopicTypeSmallVideo;


        HomeQuestionAndAnswerController *all6 =[[HomeQuestionAndAnswerController alloc]init];
        all6.type=TopicTypeQuestionOrAnswer;
        

        HomeIndustryDetailsController *all7 =[[HomeIndustryDetailsController alloc]init];
        all7.type=TopicTypeIndustryDetails;


        HomeDeliciousFoodController *all8 =[[HomeDeliciousFoodController alloc]init];
        all8.type=TopicTypeFood;
        
        _childArr =[NSMutableArray arrayWithArray:@[all1, all2, all3, all4, all5, all6, all7, all8]];
    }
    return _childArr;
}
-(SGPageContentCollectionView*)pageContentCollectionView{
    if(!_pageContentCollectionView){
        CGFloat pageTitleViewY = 44;
        CGFloat ContentCollectionViewHeight = KScreenHeight - pageTitleViewY - K_TopHeight-K_TabBarHeight;
        _pageContentCollectionView = [[SGPageContentCollectionView alloc] initWithFrame:CGRectMake(0, 44, KScreenWidth, ContentCollectionViewHeight) parentVC:self childVCs:self.childArr];
        _pageContentCollectionView.delegatePageContentCollectionView = self;
    }
    return _pageContentCollectionView;
}
-(NSMutableArray *)titleArr{
    if (!_titleArr) {
        NSArray *arr = @[@"关注", @"推荐", @"深圳", @"视频", @"小视频", @"问答", @"行业详情", @"美食"];
        _titleArr =[NSMutableArray array];
        [_titleArr addObjectsFromArray:arr];
    }
    return _titleArr;
}
@end
